const constants = require('../../../../../../common/Constants.js');
const regexUtils = require('../../../../../../utils/RegexUtils.js');
const umfDialog = require('../../../../../../utils/UmfDialog.js');
const network_utils = require('../../../../../../http/NetworkUtils.js');
import TimeUtils from '../../../../../../utils/TimeUtils';
import { saveUserCache } from '../../../../../../model/CacheUtils';

import {$Toptips} from '../../../../../../components/CompRouter';
var app = getApp();
var pageStoreId = null;
// var cacheManager = null;

Page({
    data: {
        dateArray: [],
        selectDate: '',
    },
    onLoad: function (options) {
        pageStoreId = options.storeId;
        // cacheManager = new CacheUtils.cacheManager()
        //     .init("checkForEmail", constants.STORAGE_FUNC.CHECKFOREMAIL);
        let userCache = app.globalData.userCache;

        wx.setNavigationBarTitle({
            title: (options.title + '对账邮件' || "")
        });

        let array = [];
        for (let i = 0; i < 6; i++) {
            let dateF = TimeUtils.getLatestMonth(i, "yyyy年MM月");
            array.push(dateF);
        }

        this.setData({
            dateArray: array,
            cacheEmail: userCache["agentEmail"]? userCache["agentEmail"]: "",
        });
    },


    tapView: function () {

    },


    bindChange: function (e) {
        let selectIndex = e.detail.value;
        this.setData({
            selectDate: this.data.dateArray[selectIndex]
        })
    },


    InfoSubmit: function (e) {
        let formValues = e.detail.value;
        let errMsg = this._checkFrom(formValues);
        if (errMsg) {
            $Toptips.error({text: errMsg});
        } else {
            //校验通过，缓存代理商邮箱
            saveUserCache({agentEmail: formValues["linkMan"]});
            //发送邮件
            this._requestSendMail(formValues);
        }
    },

    _checkFrom(params) {
        let resultErrorMsg = regexUtils.regexTypeWithMsg(params["linkMan"], constants.RegexTypes.LINKTELEMAIL);
        if (resultErrorMsg) {
            return resultErrorMsg;

        }
        if (!this.data.selectDate) {
            return "请选择账单月份";
        }
        return "";
    },

    _requestSendMail(fromValues) {
        let params = {
            storeId: pageStoreId,
            email: fromValues["linkMan"],
            billMonth: formatParamsDate(this.data.selectDate)
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_BILLEMAIL, params, {
            doHttpSucess: function (data) {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    // cacheManager.cacheFunc({"linkMan": formValues["linkMan"]});
                    umfDialog.show("对账邮件已发送，请查收", () => {
                        wx.navigateBack({delta: 1});
                    });
                    return true;
                }
                return false;
            }
        }).send();
    }
});

function formatParamsDate(dateStr) {
    let month = dateStr.substring(5, 7);
    var date = dateStr.substring(0, 4) + "-" + month;
    return date;
}